import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from '@/utils/store'
import { call } from '@/utils/request'
import { showMsg, showLoading, hideLoading, showSuccess, showBookError } from '@/utils/toast'


let page_num;
const page_size = 10;
let orderMap
let isLoading
Component({
  behaviors: [storeBindingsBehavior],
  data: {
    orderList: [],
    isRefreshPull: false,
    isFinish: false,
    isEmpty: false,
  },
  storeBindings: {
    store,
    fields: ['window_info'],
  },
  methods: {
    async init() {
      showLoading()
      page_num = 1;
      orderMap = {};
      const res = await this.getOrderBookPage()
       this.setData({
        orderList: res,
        isEmpty: res.length === 0 ? true : false,
        isRefreshPull: false,
      })
      hideLoading()
    },
    async getOrderBookPage() {
      let {page_num: _page_num, order_book_list} = await call('/getOrderBookListRefund/' + page_num + '/' + page_size)
      page_num = _page_num


      // 一定要先判断，原因：下面会对order_book_list进行filter去重 ！！！！！！！！！！！！！！！！
      if (order_book_list.length < page_size) {
        this.setData({
          isFinish: true
        })
      } else {
        this.setData({
          isFinish: false
        })
      }


        order_book_list = order_book_list.filter(item => {
          if (orderMap[item.id]) {
            return false;
          }
          orderMap[item.id] = true

          const t1 = item.book_start_time
          const t2 = item.book_end_time
          item.time_str = (t1 < 10 ? "0"+t1 : ""+t1) + ":00~"+(t2 < 10 ? "0"+t2 : ""+t2) + ":00"
          return true;
        })


        return order_book_list;
    },
    async onScrollBottom() {
      if (this.data.isFinish || isLoading) return
      isLoading = true
      showLoading()
      var res = await this.getOrderBookPage()
      res = this.data.orderList.concat(res)
      this.setData({
        orderList: res,
        isRefreshPull: false
      })
      hideLoading()
      isLoading = false
    },
  }
})